class TourGroupsController < ApplicationController
  
  before_filter :authorize
  
  #before_filter :list
  
  # GET /admin_tour_groups
  # GET /admin_tour_groups.xml
#  def list
#    @tour_groups = TourGroup.find(:all, {:order => "created_at desc"})
#  end
  
  def index
    klass = params[:klass]
    id = params[:id]
    unless klass.nil? || id.nil?
      obj = klass.to_s.classify.constantize.find(id)
      @obj_name = obj.name
      @tour_groups = obj.tour_groups
    else
      @tour_groups = TourGroup.find(:all, {:order => "created_at desc"})      
    end
    @all_people_count = 0
    @all_children_count = 0
    @all_total_profit = 0
    @tour_groups.each do |tour_group|
      @all_people_count += tour_group.people unless tour_group.people.nil?
      @all_children_count += tour_group.children unless tour_group.children.nil?
      @all_total_profit += tour_group.total_profit unless tour_group.total_profit.nil?
    end
  end
  
  
  def show
    @tour_group = TourGroup.find(params[:id])
  end
  
  def new
    @tour_group = TourGroup.new
    @tour_group.sequence = TourGroup.next_sequence()
    @lodging = Lodging.new
    @guide_involved = GuideInvolved.new
    @vehicle = Vehicle.new
    @outer_expense = OuterExpense.new
    @shopping = Shopping.new
    @shopping_record_1, @shopping_record_2, @shopping_record_3, @shopping_record_4, @shopping_record_5 = Array.new(5) {|i| ShoppingRecord.new}
  end
  
  def edit
    @tour_group = TourGroup.find(params[:id])
    @lodging = @tour_group.lodging
    #@lodging = Lodging.new if @lodging.nil?
    @guide_involved = @tour_group.guide_involved
    @vehicle = @tour_group.vehicle
    @outer_expense = @tour_group.outer_expense
    @shopping = @tour_group.shopping
    @shopping_record_1 = @tour_group.shopping.shopping_records[0] if @tour_group.shopping.shopping_records.count > 0
    @shopping_record_1 = ShoppingRecord.new if @shopping_record_1.nil?
    @shopping_record_2 = @tour_group.shopping.shopping_records[1] if @tour_group.shopping.shopping_records.count > 1
    @shopping_record_2 = ShoppingRecord.new if @shopping_record_2.nil?
    @shopping_record_3 = @tour_group.shopping.shopping_records[2] if @tour_group.shopping.shopping_records.count > 2
    @shopping_record_3 = ShoppingRecord.new if @shopping_record_3.nil?
    @shopping_record_4 = @tour_group.shopping.shopping_records[3] if @tour_group.shopping.shopping_records.count > 3
    @shopping_record_4 = ShoppingRecord.new if @shopping_record_4.nil?
    @shopping_record_5 = @tour_group.shopping.shopping_records[4] if @tour_group.shopping.shopping_records.count > 4
    @shopping_record_5 = ShoppingRecord.new if @shopping_record_5.nil?
  end
  
  def create
    @tour_group = TourGroup.new(params[:tour_group])
    # 处理关联对象
    # 国籍
    @tour_group.nationality = Nationality.find_or_create_by_name(params[:nationality][:name]) unless params[:nationality][:name].blank?
    # 客户
    @tour_group.customer = Customer.find_or_create_by_name(params[:customer][:name]) unless params[:customer][:name].blank?
    # 负责人
    @tour_group.owner = Employee.find_or_create_by_name(params[:owner][:name]) unless params[:owner][:name].blank?
    # 住宿
    @lodging = Lodging.new(params[:lodging])
    @tour_group.lodging = @lodging
    # ...酒店
    @lodging.hotel = Hotel.find_or_create_by_name(params[:hotel][:name]) unless params[:hotel][:name].blank?
    # 导游相关
    @guide_involved = GuideInvolved.new(params[:guide_involved])
    @tour_group.guide_involved = @guide_involved
    # ...导游
    @guide_involved.guide = Guide.find_or_create_by_name(params[:guide][:name]) unless params[:guide][:name].blank?
    # 用车相关
    @vehicle = Vehicle.new(params[:vehicle])
    @tour_group.vehicle = @vehicle
    # ...司机
    @vehicle.driver = Driver.find_or_create_by_name(params[:driver][:name]) unless params[:driver][:name].blank?
    # ...车型
    @vehicle.vehicle_model = VehicleModel.find_or_create_by_name(params[:vehicle_model][:name]) unless params[:vehicle_model][:name].blank?
    # 进店相关
    @shopping = Shopping.new(params[:shopping])
    @tour_group.shopping = @shopping
    # 获取 5 个进店记录
    @shopping_record_1 = ShoppingRecord.new
    set_shopping_record(@shopping_record_1, "shopping_record_1")
    @shopping_record_2 = ShoppingRecord.new
    set_shopping_record(@shopping_record_2, "shopping_record_2")
    @shopping_record_3 = ShoppingRecord.new
    set_shopping_record(@shopping_record_3, "shopping_record_3")
    @shopping_record_4 = ShoppingRecord.new
    set_shopping_record(@shopping_record_4, "shopping_record_4")
    @shopping_record_5 = ShoppingRecord.new
    set_shopping_record(@shopping_record_5, "shopping_record_5")
    # 外地支出
    @outer_expense = OuterExpense.new(params[:outer_expense]) 
    @tour_group.outer_expense = @outer_expense
    
    if @tour_group.save
      flash[:notice] = "#{@tour_group.number}团队收支表创建成功。"
      redirect_to tour_groups_url
    else
        render :action => "new"
    end
  end
  
  def update
    @tour_group = TourGroup.find(params[:id])
    # 处理关联对象
    # 国籍
    params[:tour_group][:nationality] = Nationality.find_or_create_by_name(params[:nationality][:name]) unless params[:nationality][:name].blank?
    # 客户
    params[:tour_group][:customer] = Customer.find_or_create_by_name(params[:customer][:name]) unless params[:customer][:name].blank?
    # 负责人
    params[:tour_group][:owner] = Employee.find_or_create_by_name(params[:owner][:name]) unless params[:owner][:name].blank?
    # 住宿    
    # ...酒店
    params[:lodging][:hotel] = Hotel.find_or_create_by_name(params[:hotel][:name]) unless params[:hotel][:name].blank?
    @tour_group.lodging.update_attributes(params[:lodging])
    @lodging = @tour_group.lodging
    # 导游相关
    # ...导游
    params[:guide_involved][:guide] = Guide.find_or_create_by_name(params[:guide][:name]) unless params[:guide][:name].blank?
    @tour_group.guide_involved.update_attributes(params[:guide_involved])
    @guide_involved = @tour_group.guide_involved
    # 用车相关
    # ...司机
    params[:vehicle][:driver] = Driver.find_or_create_by_name(params[:driver][:name]) unless params[:driver][:name].blank?
    # ...车型
    params[:vehicle][:vehicle_model] = VehicleModel.find_or_create_by_name(params[:vehicle_model][:name]) unless params[:vehicle_model][:name].blank?
    @tour_group.vehicle.update_attributes(params[:vehicle])
    @vehicle = @tour_group.vehicle
    # 进店相关
    @tour_group.shopping.update_attributes(params[:shopping])
    @shopping = @tour_group.shopping
    # ...删除所有进店记录
    @shopping.shopping_records.destroy_all
    # 获取 5 个进店记录
    @shopping_record_1 = ShoppingRecord.new
    set_shopping_record(@shopping_record_1, "shopping_record_1")
    @shopping_record_2 = ShoppingRecord.new
    set_shopping_record(@shopping_record_2, "shopping_record_2")
    @shopping_record_3 = ShoppingRecord.new
    set_shopping_record(@shopping_record_3, "shopping_record_3")
    @shopping_record_4 = ShoppingRecord.new
    set_shopping_record(@shopping_record_4, "shopping_record_4")
    @shopping_record_5 = ShoppingRecord.new
    set_shopping_record(@shopping_record_5, "shopping_record_5")    
    # 外地支出
    @tour_group.outer_expense.update_attributes(params[:outer_expense])
    @outer_expense = @tour_group.outer_expense
    
    if @tour_group.update_attributes(params[:tour_group])
      flash[:notice] = "#{@tour_group.number}团队收支表修改成功。"
      redirect_to(@tour_group)
    else
      render :action => "edit"
    end
  end
  
  def destroy
    @tour_group = TourGroup.find(params[:id])
    @tour_group.destroy
    flash[:notice] = "#{@tour_group.number}团队收支表删除成功。"
    redirect_to tour_groups_url
  end
  
  def destroy_all
    TourGroup.destroy_all
    redirect_to tour_groups_url
  end
  #  def list_object
  #    object = params[:object]
  #    method = params[:method]
  #    options = {:order => "#{method} ASC"}
  #    @objects = object.to_s.camelize.constantize.find(:all, options )
  #    
  #    render :partial => "/shared/objects", :locals => { :objects => @objects, :method => method}, :layout => false
  #  end
  
  # 备份数据
#  def backup
#    data = YAML.dump_stream(Shop.find(:all))
#    send_data data, :filename => "data_#{DateTime.now.strftime("%F")}.bak"
#  end
  
  # 恢复数据
#  def restore
#    puts "restore...."
#  end
  
  private
  def set_shopping_record(record, param_name)
    shop_name = params[param_name][:shop][:name]
    params[param_name].delete(:shop)
    
    record.attributes = params[param_name]
    #puts record.inspect
    record.shop = Shop.find_or_create_by_name(shop_name) unless shop_name.blank?
    @shopping.shopping_records << record unless record.shop.nil?
  end
end
